import sys
import threading
import math
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QColor, QPen

class RotatingSpinnerDialog(QDialog):
    def __init__(self, parent=None, radius=20):
        super().__init__(parent)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setModal(True)
        self.setFixedSize(100, 100)

        self.angle = 0
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_spinner)
        self.timer.start(50)  # 每 50ms 更新一次（越小转得越快）

    def update_spinner(self):
        self.angle = (self.angle + 30) % 360
        self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # 设置画笔
        pen = QPen(QColor(255, 0, 0), 5)  # 红色线条，5px 粗
        painter.setPen(pen)

        # 计算圆心和半径
        center_x = self.width() / 2
        center_y = self.height() / 2
        radius = 30

        # 画旋转的线段（类似时钟秒针）
        for i in range(12):
            alpha = int(255 * (i + 1) / 12)
            pen.setColor(QColor(255, 0, 0, alpha))
            painter.setPen(pen)

            angle_deg = (self.angle + i * 30) % 360
            angle_rad = math.radians(angle_deg)

            x1 = center_x + math.cos(angle_rad) * (radius - 10)
            y1 = center_y + math.sin(angle_rad) * (radius - 10)
            x2 = center_x + math.cos(angle_rad) * radius
            y2 = center_y + math.sin(angle_rad) * radius
            painter.drawLine(int(x1), int(y1), int(x2), int(y2))

    def start(self):
        self.show()

    def stop(self):
        self.timer.stop()
        self.accept()